package com.ysd.lis.service.qc.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.qc.QcMaterialSummary;
import com.ysd.lis.mapper.qc.QcMaterialSummaryMapper;
import com.ysd.lis.request.qc.QcSummaryParam;
import com.ysd.lis.service.qc.QcMaterialSummaryService;
import com.ysd.util.ToolsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2025-05-13
 */
@Service
@Slf4j
public class QcMaterialSummaryServiceImpl extends ServiceImpl<QcMaterialSummaryMapper, QcMaterialSummary> implements QcMaterialSummaryService {

    @Autowired
    QcMaterialSummaryMapper qcMaterialSummaryMapper;

    @Override
    public Result getQcMaterialSummary(QcSummaryParam param) {
        //根据仪器id和月份查询总结数据
        MPJLambdaWrapper<QcMaterialSummary> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(QcMaterialSummary.class)
                .eq(QcMaterialSummary::getInstrId,param.getInstrId())
                .eq(QcMaterialSummary::getQcMonth,param.getQcMonth())
                .eq(QcMaterialSummary::getDelFlag,0);
        List<QcMaterialSummary> qcMaterialSummaries = qcMaterialSummaryMapper.selectList(queryWrapper);
        if(ToolsUtils.isNotEmpty(qcMaterialSummaries)){
            QcMaterialSummary qcMaterialSummary = qcMaterialSummaries.get(0);
            return Result.succ(qcMaterialSummary);
        }else {
            return Result.succ(null);
        }
    }

    @Override
    public Result addMaterialSummary(QcMaterialSummary supplier) {
        int insert = qcMaterialSummaryMapper.insert(supplier);
        if(insert>0){
            return Result.succ(1,"添加成功",supplier);
        }else {
            return Result.fail(400,"添加失败","");
        }
    }

    @Override
    public Result editMaterialSummary(QcMaterialSummary supplier) {
        int updateById = qcMaterialSummaryMapper.updateById(supplier);
        if(updateById>0){
            return Result.succ(1,"提交成功",supplier);
        }else {
            return Result.fail(400,"修改失败","");
        }
    }
}
